s = '伟大中国梦'
# 编码 str --> bytes
scode = s.encode(errors='replace')
# 默认是utf-8，因为utf-8中文占3个字节
print(scode)    # b'\xe4\xbc\x9f\xe5\xa4\xa7\xe4\xb8\xad\xe5\x9b\xbd\xe6\xa2\xa6'

scode_gbk = s.encode('gbk', errors='replace')
# gbk中文占2个字节
print(scode_gbk)    # b'\xce\xb0\xb4\xf3\xd6\xd0\xb9\xfa\xc3\xce'

# 编码中的出错问题
s2 = '耶✌'
scode = s2.encode('gbk', errors='ignore')
print(scode)    # b'\xd2\xae'

s3 = '耶✌'
# scode2 = s3.encode('gbk', errors='strict')
# print(scode2)
# UnicodeEncodeError: 'gbk' codec can't encode character '\u270c' in position 1: illegal multibyte sequence

s4 = '耶✌'
scode3 = s4.encode('gbk', errors='replace')
print(scode3)   # b'\xd2\xae?'

# 解码过程 bytes -> str
print(bytes.decode(scode_gbk, 'gbk'))   # 伟大中国梦
print(bytes.decode(scode, 'gbk'))   # 耶

